package com.zys.el.time.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zys.el.time.common.exception.sql.SqlException;
import com.zys.el.time.entity.ThinkingLike;
import com.zys.el.time.mapper.ThinkingLikeMapper;
import com.zys.el.time.service.ThinkingLikeService;
import org.springframework.stereotype.Service;


@Service
public class ThinkingLikeServiceImpl extends ServiceImpl<ThinkingLikeMapper, ThinkingLike>
        implements ThinkingLikeService {

    @Override
    public boolean isLike(Long thinkingId, Long userId) {
        Long count = this.query().eq("thinking_id", thinkingId).eq("user_id", userId).count();
        return count > 0;
    }

    @Override
    public void changeLikeStatus(Long thinkingId, Long userId, boolean flag) {
        if (flag) { // 点赞
            ThinkingLike thinkingLike = new ThinkingLike();
            thinkingLike.setThinkingId(thinkingId);
            thinkingLike.setUserId(userId);
            this.save(thinkingLike);
        } else { // 取消点赞
            QueryWrapper<ThinkingLike> qw = new QueryWrapper<>();
            qw.lambda().eq(ThinkingLike::getThinkingId, thinkingId).eq(ThinkingLike::getUserId, userId);
            this.remove(qw);
        }

    }
}




